package com.imti.controller;

import com.imti.mapper.UserMapper;
import com.imti.model.User;
import com.imti.service.UserService;
import com.imti.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/users")
public class UserController {


    @Autowired
    private UserService userService;
    // 注入RedisTemplate
    private final RedisTemplate<String, Object> redisTemplate;
    @Resource
    UserMapper userMapper;

    public UserController(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @RequestMapping
    public String login(){
        return "/user/login";
    }

    /**
     *登录
     * @param user
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginin(User user,PageBean page, HttpServletRequest request){
        User user1 = userService.verifyLogin(user);
        request.getSession().setAttribute("userId",user1);
            if(user1!=null){
                if ("0".equals(user1.getLockFlag())){
                    if ("1".equals(user1.getRole())){
                        return "redirect:###";
                    }/*else if ("2".equals(user1.getRole())){
                request.setAttribute("userId",userService.selectAllStu());
                return "user/user-list";
            }*/
                    else if ("3".equals(user1.getRole())||"2".equals(user1.getRole())){
                        redisTemplate.opsForValue().set("username",user1.getUsername());
                        redisTemplate.opsForValue().set("userId",user1.getId());
                        return "redirect:/users/allUsers";
                    }
                }
            }else {
                return "redirect:/users";
            }

        return "redirect:/users";
    }

    @RequestMapping("/bind")
    public String bind(HttpServletRequest request,@RequestParam("clazzId") Integer id){
        String[] selectedIds = request.getParameterValues("selectedIds");
        userService.updateStuClaz(selectedIds,id);
        return "redirect:/users/allUsers";
    }

    @RequestMapping("/register")
    public String register(){
        return "/user/register";
    }

    @RequestMapping("/reg")
    public String reg(User user){
        userService.registed(user);
        return "redirect:/users";
    }

    @RequestMapping("/validname")
    @ResponseBody
    public int registed(String username){
        return userService.validname(username);
    }

    @RequestMapping("/allUsers")
    public String findAllUser(User user,PageBean page, HttpServletRequest request){
        request.setAttribute("page",userService.userPage(user,page));
        return "user/user-list";
    }

    //先跳转页面
    @RequestMapping("/addUser")
    public String adduser(){
        return "user/user-add";
    }
    //执行添加操作
    @RequestMapping("/add")
    public String addUserById(User user){
        userService.addUserById(user);
        System.out.println(user);
        return "redirect:/users/allUsers";
    }

    @RequestMapping("/verifyPhone")
    @ResponseBody
    public int vPhone(String phone){
        return userService.vPhone(phone);
    }

    @RequestMapping("/updateUser/{id}")
    public String updateuser(@PathVariable("id") Integer id, Model model){
        User user = userService.findUserById(id);
        model.addAttribute(user);
        return "user/user-update";
    }
    @RequestMapping("/update")
    public String updateUser(User user){
        System.out.println(user.getLockFlag());
        userService.UpdateUser(user);
        return "redirect:/users/allUsers";
    }

    @RequestMapping("/res")
    @ResponseBody
    public String res(Integer id){
        boolean b = userService.resPassword(id);
        if (b){
            return "{\"code\":\"1\"}";
        }else {
            return "{\"code\":\"0\"}";
        }

    }
    @GetMapping("/valid")
    @ResponseBody  //ajax--get 异步
    public String getUserName(String name){
        return userMapper.getUserName(name)>0?"1":"0";
    }
    @GetMapping("/toreg")
    public String toreg() {
        return "user/register";
    }

    @RequestMapping("/page")
    public String findAll(User user, PageBean page, HttpServletRequest request) {
        request.setAttribute("page",userService.userPage(user,page));
        return "user/user-list";
    }



}
